import 'package:yp/config/config.dart';
import 'package:yp/pages/main/logic.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import '../../helper.dart';
import 'state.dart';

class orderCompleteLogic extends GetxController {
  final state = orderCompleteState();
  // final MainAppLogic MainAppLogics = Get.put(MainAppLogic());
  final MainLogic mainLogic = Get.put(MainLogic());
  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    getrecommendGoods();
    getAdvertising();
    // var params = {
    //   'tml_num_id':SpUtil.getInt('tml_num_id'),  // 网格员
    // };
    // Api.paymentResult(params).listen((event) {
    //
    //   update();
    // });
  }

// 获取广告内容
  void getAdvertising() {
    var params = {
      'adv_category': '4',
      'page_num': 1,
      'page_size': 10,
      'status_num_id': 3
    };
    Api.getAdvertising(params).listen((event) {
      if (event["code"] == 0 && event["list"] != null) {
        state.advertising = event['list'];
      } else {
        state.advertising = [];
      }
      update();
    });
  }

  getrecommendGoods() {
    var params = {
      "usr_num_id": SpUtil.getInt('empeNumId'),
      'access_token': SpUtil.getString('token'),
      "use_platform": 2,

      "is_recommend": 1, //死值,推荐商品
      // "shop_id": SpUtil.getInt('storeShopNum'),
      "shop_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      // "prv_num_id": "320000",
      // "city_num_id": "320500",
      // "city_area_num_id": "320583",
      "page_no": 1,
      "page_size": 10,
      "order_by": "sold_quantity desc"
    };
    Api.recommendGoods(params).listen((event) {
      if (event["code"] == 0) {
        if (event['lists'] != null && event['lists'].length > 0) {
          state.TJlist = event['lists'];
        }
        update();
      }
    });
  }

  getMoney(data) {
    String priceStr = '', oldPriceStr = '', tiShi = '';
    bool oldBool = false;
    if (data['is_seckill'] == 1) {
      // 秒杀商品
      priceStr = data['price'].toString();
      oldBool = data['mkt_price'] > data['price']; // 显示旧价格
      oldPriceStr = data['mkt_price'].toString();
    } else if (data['is_pre_sale'] == 1) {
      priceStr = data['price'].toString();
      oldBool = data['mkt_price'] > data['price']; // 显示旧价格
      oldPriceStr = data['mkt_price'].toString();
    } else {
      if (data['expect_price'] > 0) {
        priceStr = '${data['expect_price'].toString()}';
        tiShi = '折后';
        oldBool = data['mkt_price'] > data['expect_price']; // 显示旧价格
        oldPriceStr = data['mkt_price'].toString();
      } else {
        priceStr = data['price'].toString();
        oldBool = data['mkt_price'] > data['price']; // 显示旧价格
        oldPriceStr = data['mkt_price'].toString();
      }
    }
    return [priceStr, oldPriceStr];
  }

  // 添加购物车
  void addShoppingCart({
    String ec_shop_num_id,
    String item_num_id,
    String style_num_id,
    String ec_style_num_id,
  }) {
    var params = {
      "use_platform": 2, //平台
      "usr_num_id": SpUtil.getInt('empeNumId'), //会员ID
      "emp_num_id": 0, //操作员ID,移动端无属性,默认传0
      "channel_num_id": 99, //订单渠道：小程序 98，App 99
      "quantity": 1.0, //数量
      "ec_shop_num_id": ec_shop_num_id, //店铺编码
      "item_num_id": item_num_id, //商品编码
      "style_num_id": style_num_id, //款式编码
      "ec_style_num_id": ec_style_num_id, //款式唯一编码
      'access_token': SpUtil.getString('token'),
      // "prv_num_id": 320000, //省编码
      // "city_num_id": 320500, //市编码
      // "city_area_num_id": 320583, //区编码
    };
    Api.addShoppingCart(params).listen((event) {
      if (event['code'] == 0) {
        mainLogic.getCartNum();
        toast('添加成功');
      } else {
        toast(event['message']);
      }
    });
  }
}
